package com.ciba.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ciba.demo.mapper.UserInfoMapper;
import com.ciba.demo.pojo.UserInfo;
import com.ciba.demo.respone.ApiRestResponse;
import com.ciba.demo.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Repository
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    UserInfoMapper userInfoMapper ;

    @Override
    public ApiRestResponse getUserInfo(HttpServletRequest request, HttpServletResponse response) {

        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("user");
        userInfo = userInfoMapper.selectById(userInfo.getId()) ;
        return ApiRestResponse.success().setStatus(1).setMsg("获取成功").setData(userInfo);
    }

    @Override
    public ApiRestResponse changeUserInfo(HttpServletRequest request, HttpServletResponse response) {
        UserInfo user = (UserInfo)request.getSession().getAttribute("user") ;
        UserInfo userInfo = new UserInfo() ;
        String email = request.getParameter("email") ;
        String phone = request.getParameter("phone") ;
        String name = request.getParameter("name") ;
        String password = request.getParameter("password") ;

        userInfo.setEmail(email);
        userInfo.setPhone(phone);
        userInfo.setName(name);
        userInfo.setPassword(password);

        UpdateWrapper<UserInfo> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",user.getId()) ;
        int ans = userInfoMapper.update(userInfo,wrapper) ;
        if(ans==1){
            return  ApiRestResponse.success().setStatus(1).setMsg("成功！");
        }else{
            return  ApiRestResponse.error().setMsg("失败！").setStatus(0) ;
        }
    }
}
